package com.ruoyi.goods.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.goods.domain.CommodityProductDetails;
import com.ruoyi.goods.service.ICommodityProductDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author kzc
 * @date 2024-03-09
 */
@RestController
@RequestMapping("/goods/commodity")
public class CommodityProductDetailsController extends BaseController
{
    @Autowired
    private ICommodityProductDetailsService commodityProductDetailsService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CommodityProductDetails commodityProductDetails)
    {
        startPage();
        List<CommodityProductDetails> list = commodityProductDetailsService.selectCommodityProductDetailsList(commodityProductDetails);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityProductDetails commodityProductDetails)
    {
        List<CommodityProductDetails> list = commodityProductDetailsService.selectCommodityProductDetailsList(commodityProductDetails);
        ExcelUtil<CommodityProductDetails> util = new ExcelUtil<CommodityProductDetails>(CommodityProductDetails.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Integer productId)
    {
        return success(commodityProductDetailsService.selectCommodityProductDetailsByProductId(productId));
    }

    /**
     * 新增商品
     */
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityProductDetails commodityProductDetails)
    {
        return toAjax(commodityProductDetailsService.insertCommodityProductDetails(commodityProductDetails));
    }

    /**
     * 修改商品
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityProductDetails commodityProductDetails)
    {
        return toAjax(commodityProductDetailsService.updateCommodityProductDetails(commodityProductDetails));
    }

    /**
     * 删除商品
     */
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Integer[] productIds)
    {
        return toAjax(commodityProductDetailsService.deleteCommodityProductDetailsByProductIds(productIds));
    }
    //批量修改
    @DeleteMapping("/addFavor/{productIds}")
    public AjaxResult addoptimization(@PathVariable Integer[] productIds)
    {

        for (Integer productId : productIds) {
            commodityProductDetailsService.addoptimization(productId);
        }
        return AjaxResult.success("成功");
    }
    //批量修改
    @DeleteMapping("/delFavor/{productIds}")
    public AjaxResult deleteInBulk(@PathVariable Integer[] productIds)
    {

        for (Integer productId : productIds) {
            commodityProductDetailsService.deleteInBulk(productId);
        }
        return AjaxResult.success("成功");
    }

}
